package com.group_e.Bubble.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.group_e.Bubble.client.NotLoggedInException;
import com.group_e.Bubble.client.Task;
import com.group_e.Bubble.client.UserServiceB;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class TaskServiceImpl extends RemoteServiceServlet implements UserServiceB {
	private static final Logger LOG = Logger.getLogger(TaskServiceImpl.class.getName());
	private static final PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("transactions-optional");
	private List<Date> task_date = new ArrayList<Date>();
	private List<String> task_descrip = new ArrayList<String>();
	private List<String> task_tag = new ArrayList<String>();
	private List<Date> task_duedate = new ArrayList<Date>();
	private List<Date> task_removedate = new ArrayList<Date>();
	private List<Date> task_completedate = new ArrayList<Date>();
			
	
	public void addTask(String name) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			pm.makePersistent(new Tasks(getUser(), name));
		}
		finally {
			pm.close();
		}
	}

	private void checkLoggedIn() throws NotLoggedInException {
		if (getUser() == null) {
			throw new NotLoggedInException("Not logged in.");
		}
	}
	private User getUser() {
		UserService userService = UserServiceFactory.getUserService();
		return userService.getCurrentUser();
	}
	private PersistenceManager getPersistenceManager() {
		return PMF.getPersistenceManager();
	}

	public void removeTask(Long id) throws NotLoggedInException {
		// TODO Auto-generated method stub
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			long deleteCount = 0;
			Query q = pm.newQuery(Tasks.class, "user == u");
			q.declareParameters("com.google.appengine.api.users.User u");
			List<Tasks> tasks = (List<Tasks>) q.execute(getUser());
			for(Tasks task : tasks) {
				if (id == (task.getId())){
					deleteCount++;
					pm.deletePersistent(task);
				}
			}
			if(deleteCount !=1) {
				LOG.log(Level.WARNING, "Task Removed");
			}
		} finally {
			pm.close();
		}
	}

	@Override
	public String[] getTask_Name() throws NotLoggedInException {
		// TODO Auto-generated method stub
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		List<String> task_name = new ArrayList<String>();
		//List<Date> task_date = new ArrayList<Date>();
		//List<String> task_descrip = new ArrayList<String>();
		//List<String> task_tag = new ArrayList<String>();
		//List<Date> task_duedate = new ArrayList<Date>();
		//List<Date> task_removedate = new ArrayList<Date>();
		//List<Date> task_completedate = new ArrayList<Date>();
		try {
			Query q = pm.newQuery(Tasks.class, "user == u");
			q.declareParameters("com.google.appengine.api.users.User u");
			q.setOrdering("dueDate");
			List<Tasks> task = (List<Tasks>) q.execute(getUser());
			for(Tasks tasks: task) {
				task_name.add(tasks.getName());
			}
		} finally {
			pm.close();
		}
		return (String[]) task_name.toArray(new String[0]);
	}

}
	

